package com.tanhua.dubbo.api;

import com.mongodb.client.result.DeleteResult;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.service.CommentApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @author げっしょく
 * @date 2021/10/27
 */
@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Integer save(Comment comment) {
        //查询动态数据
        Movement movement = mongoTemplate.findOne(Query.query(Criteria.where("_id").is(comment.getPublishId())), Movement.class);
        //设置被评论人的id
        comment.setPublishUserId(movement.getUserId());
        //保存评论到数据库
        mongoTemplate.save(comment);

        //查询需要更新的数据
        Query q = Query.query(Criteria.where("_id").is(comment.getPublishId()));

        //创建更新对象, 更新的字段
        Update update = new Update();
        switch (comment.getCommentType()) {
            case 1:
                update.inc("likeCount", 1);
                break;
            case 2:
                update.inc("commentCount", 1);
                break;
            case 3:
                update.inc("loveCount", 1);
                break;
            default:
                break;
        }
        //设置更新后的返回的是最新数据
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        findAndModifyOptions.returnNew(true);

        //更新动态的互动数据
        Movement andModify = mongoTemplate.findAndModify(q, update, findAndModifyOptions, Movement.class);
        Integer count = null;
        switch (comment.getCommentType()) {
            case 1:
                count = andModify.getLikeCount();
                break;
            case 2:
                count = andModify.getCommentCount();
                break;
            case 3:
                count = andModify.getLoveCount();
                break;
            default:
                break;
        }
        return count;
    }

    //查询动态或视频评论
    @Override
    public List<Comment> getComments(Integer page, Integer pagesize, ObjectId movementId) {
        Query query = Query.query(Criteria.where("publishId").is(movementId)).
                skip((page - 1) * pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.asc("created")));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        return comments;
    }

    //判断是否用户是否点赞或喜欢
    @Override
    public Boolean getOperation(Long userId, ObjectId movementId, CommentType commType) {
        Criteria criteria = Criteria.where("publishId").is(movementId)
                .and("userId").is(userId)
                .and("commentType").is(commType.getType());
        Query query = Query.query(criteria);
        Comment comment = mongoTemplate.findOne(query, Comment.class);
        if (comment == null) {
            return false;
        } else {
            return true;
        }

    }

    @Override
    public Integer delete(Comment comment) {
        //增加条件
        Criteria criteria = Criteria.where("publishId").is(comment.getPublishId()).and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);
        //从数据库中删除对应的操作
        mongoTemplate.remove(query, Comment.class);

        Query q = Query.query(Criteria.where("_id").is(comment.getPublishId()));
        Update update = new Update();
        switch (comment.getCommentType()) {
            case 1:
                update.inc("likeCount", -1);
                break;
            case 2:
                update.inc("commentCount", -1);
                break;
            case 3:
                update.inc("loveCount", -1);
                break;
            default:
                break;
        }

        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        findAndModifyOptions.returnNew(true);

        Movement movement = mongoTemplate.findAndModify(q, update, findAndModifyOptions, Movement.class);
        Integer count = null;
        switch (comment.getCommentType()) {
            case 1:
                count = movement.getLikeCount();
                break;
            case 2:
                count = movement.getCommentCount();
                break;
            case 3:
                count = movement.getLoveCount();
                break;
            default:
                break;
        }

        return count;
    }

    //查询该动态是否点赞
    @Override
    public Boolean getComment(ObjectId id) {
        Query publishId = Query.query(Criteria.where("publishId").is(id));
        Comment one = mongoTemplate.findOne(publishId, Comment.class);
        if (one != null) {
            return true;
        }
        return false;
    }

    //对评论进行点赞并返回总赞数
    @Override
    public Comment saveComment(Comment comment) {
        /*Query publishId = Query.query(Criteria.where("publishId").is(id));
        mongoTemplate.findAndModify(publishId, )*/
        //查找到需要进行自增的行,获得改行的数据
        Query query = Query.query(Criteria.where("_id").is(comment.getPublishId()));
        Comment comment1 = mongoTemplate.findOne(query, Comment.class);
        //拿到被人点赞的评论的评论人的id
        //将id设置为被点赞的用户id
        comment.setPublishUserId(comment1.getUserId());
        //保存数据库
        mongoTemplate.save(comment);

        //修改对象
        Update update = new Update();
        //对key的那个字段进行自增1
        update.inc("likeCount", 1);
        //设置返回的参数是最新数据
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        findAndModifyOptions.returnNew(true);

        //返回最新数据
        Comment andModify = mongoTemplate.findAndModify(query, update, findAndModifyOptions, Comment.class);
        return andModify;
    }

    //对评论进行取消点赞并返回总赞数
    @Override
    public Comment deleteComment(Comment comment) {
        //获得查询的行数的条件
        Criteria criteria = Criteria.where("publishId").is(comment.getPublishId()).and("userId").is(comment.getUserId());
        Query query = Query.query(criteria);
        //删除对应的参数
        DeleteResult remove = mongoTemplate.remove(query, Comment.class);
        //设置修改的字段-1
        Update update = new Update();
        update.inc("likeCount", -1);
        //设置返回的参数为最新数据
        FindAndModifyOptions findAndModifyOptions = new FindAndModifyOptions();
        findAndModifyOptions.returnNew(true);
        //设置更新行的数据
        Query id = Query.query(Criteria.where("_id").is(comment.getPublishId()));
        Comment andModify = mongoTemplate.findAndModify(id, update, findAndModifyOptions, Comment.class);

        return andModify;
    }

    //查找用户的互动数据
    @Override
    public List<Comment> getSubject(Integer page, Integer pagesize, Long id, CommentType commentType) {
        Criteria criteria = Criteria.where("publishUserId").is(id).and("commentType").is(commentType.getType());
        Query created = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));
        List<Comment> list = mongoTemplate.find(created, Comment.class);
        return list;
    }

    //保存视频动态
    @Override
    public Comment saveVideo(Comment comment, CommentType commentType) {
        //查找对应的视频数据
        Query q = Query.query(Criteria.where("_id").is(comment.getPublishId()));
        Video one = mongoTemplate.findOne(q, Video.class);
        //设置被评论人,评论时间,评论类型
        comment.setPublishUserId(one.getUserId());
        comment.setCreated(System.currentTimeMillis());
        switch (commentType) {
            case LIKE:
                comment.setCommentType(commentType.getType());
                break;
            case COMMENT:
                comment.setCommentType(commentType.getType());
                break;
            default:
                break;
        }
        //保存到数据库
        mongoTemplate.save(comment);
        //对视频对应的互动信息进行自增1
        Update update = new Update();
        switch (commentType) {
            case LIKE:
                update.inc("likeCount", 1);
                break;
            case COMMENT:
                update.inc("commentCount", 1);
                break;
            default:
                break;
        }
        mongoTemplate.updateFirst(q, update, Video.class);

        return comment;
    }

    //删除视频动态
    @Override
    public void deleteVideo(Comment comment, CommentType commentType) {
        //删除对应的互动消息
        Query query = Query.query(Criteria.where("publishId").is(comment.getPublishId())
                .and("userId").is(comment.getUserId()).and("commentType").is(commentType.getType()));
        mongoTemplate.remove(query, Comment.class);

        //对视频中的对应互动数减一
        Update update = new Update();
        switch (commentType) {
            case LIKE:
                update.inc("likeCount", -1);
                break;
            case COMMENT:
                update.inc("commentCount", -1);
                break;
            default:
                break;
        }
        Query q = Query.query(Criteria.where("_id").is(comment.getPublishId()));
        mongoTemplate.updateFirst(q, update, Video.class);
    }
}
